package com.core.interceptor;

import javax.servlet.http.HttpServletRequest;

import com.jfinal.aop.Interceptor;
import com.jfinal.core.ActionInvocation;
import com.jfinal.kit.StringKit;

/**
 * 配置Context,NAME_SAPCE参数拦截器
 * 
 * @author wangkai
 * @date Feb 12, 2013 11:40:03 AM
 * 
 */
public class ContextInterceptor implements Interceptor {
	private String contextPathKey = "CTX";
	private String namespaceKey = "NAME_SPACE";

	public ContextInterceptor() {
	}

	public ContextInterceptor(String base) {
		if (StringKit.isBlank(base)) {
			throw new IllegalArgumentException(
					"contextPathKey can not be blank.");
		}
		this.contextPathKey = base;
	}

	public ContextInterceptor(String base, String namespace) {
		if (!StringKit.notBlank(base, namespace)) {
			throw new IllegalArgumentException(
					"contextPathKey can not be blank.");
		}
		this.contextPathKey = base;
		this.namespaceKey = namespace;
	}

	public void intercept(ActionInvocation ai) {
		ai.invoke();
		HttpServletRequest request = ai.getController().getRequest();
		request.setAttribute(contextPathKey, request.getContextPath());
		request.setAttribute(namespaceKey, ai.getControllerKey());
	}

}
